package com.vicente.managerdemo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vicente.managerdemo.common.ResultBean;
import com.vicente.managerdemo.dto.RoleQueryDto;
import com.vicente.managerdemo.dto.RoleVO;
import com.vicente.managerdemo.entity.Menu;
import com.vicente.managerdemo.entity.Role;
import com.vicente.managerdemo.entity.User;
import com.vicente.managerdemo.mapper.RoleMapper;
import com.vicente.managerdemo.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author vicente
 * @since 2024-12-04
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public ResultBean queryPage(RoleQueryDto queryDto) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(queryDto.getRoleName())){
            queryWrapper.lambda().like(Role::getRoleName, queryDto.getRoleName());
        }
        if (StringUtils.isNotEmpty(queryDto.getRoleKey())){
            queryWrapper.lambda().like(Role::getRoleKey, queryDto.getRoleKey());
        }
        Page<Role> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        Page<Role> userPage = roleMapper.selectPage(page, queryWrapper);
        return ResultBean.getSuccess(userPage);
    }

    @Override
    public ResultBean insertRole(Role role) {
        LambdaQueryWrapper<Role> queryName = new QueryWrapper<Role>().lambda().eq(Role::getRoleName, role.getRoleName());
        if (!roleMapper.selectList(queryName).isEmpty()) {
            return ResultBean.getFail("存在同名角色,新增失败");
        } else {
            int insert = roleMapper.insert(role);
            if (insert > 0) {
                return ResultBean.getSuccess("新增成功");
            }
            return ResultBean.getFail("新增失败");
        }
    }

    @Override
    public ResultBean updateRole(Role role) {
        int update = roleMapper.updateById(role);
        if (update > 0) {
            return ResultBean.getSuccess("更新成功");
        }
        return ResultBean.getFail("更新失败");
    }

    @Override
    public ResultBean deleteById(int id) {
        int delete = roleMapper.deleteById(id);
        if (delete > 0) {
            return ResultBean.getSuccess("删除成功");
        }
        return ResultBean.getFail("删除失败");
    }

    @Override
    public ResultBean getAllRoles() {
        return ResultBean.getSuccess(roleMapper.selectList(new QueryWrapper<>()));
    }

    @Override
    public List<Long> listUserIdsByRoleId(Long roleId) {
        return roleMapper.listUserIdsByRoleId(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserRole(Long roleId, List<Long> userIds) {
        roleMapper.deleteUserRoleByRoleId(roleId);
        if (userIds != null && !userIds.isEmpty()){
            roleMapper.insertUserRole(roleId,userIds);
        }
    }

    @Override
    public List<Menu> listMenusByRoleId(Long roleId) {
        return roleMapper.listMenusByRoleId(roleId);
    }

    @Override
    public boolean checkRoleNameUnique(RoleVO sysRoleVO) {
        Long roleId = ObjectUtils.isEmpty(sysRoleVO.getRoleId()) ? NumberUtils.LONG_MINUS_ONE : sysRoleVO.getRoleId();
        Role entity = roleMapper.checkRoleNameUnique(sysRoleVO.getRoleName());
        return ObjectUtils.isEmpty(entity) || entity.getRoleId().longValue() == roleId.longValue();
    }

    @Override
    public boolean checkRoleKeyUnique(RoleVO sysRoleVO) {
        Long roleId = ObjectUtils.isEmpty(sysRoleVO.getRoleId()) ? NumberUtils.LONG_MINUS_ONE : sysRoleVO.getRoleId();
        Role entity = roleMapper.checkRoleKeyUnique(sysRoleVO.getRoleKey());
        return ObjectUtils.isEmpty(entity) || entity.getRoleId().longValue() == roleId.longValue();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleAndRoleMenu(RoleVO sysRoleVO) {
        if (sysRoleVO.getRoleId() != null) {
            roleMapper.updateById((Role)sysRoleVO);
        }else {
            roleMapper.insert((Role)sysRoleVO);
        }
        //变更角色菜单管理表数据，先删后增
        if (CollectionUtil.isNotEmpty(sysRoleVO.getMenuIds())) {
            roleMapper.deleteRoleMenuByRoleId(sysRoleVO.getRoleId());
            roleMapper.insertRoleMenu(sysRoleVO.getRoleId(), sysRoleVO.getMenuIds());
        }
    }

}
